package com.user.service

import com.data.bean.ThemeScreenshot
import com.data.entity.ThemeScreenshotUtils
import com.data.util.CheckDataUtils
import com.data.util.DatabaseIdUtils
import com.data.util.FinalStringUtils
import com.data.util.ImageLookUtils
import com.user.dao.ThemeScreenshotAdminDao
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.multipart.commons.CommonsMultipartFile

import java.util.Date

/**
 * FileName: ThemeScreenshotAdminService
 * Author:   HingLo
 * Date:     2017/11/6 12:02
 * Description: 截图管理的服务层
 */
@Service
class ThemeScreenshotAdminService {
    @Autowired
    var themeScreenshotAdminDao: ThemeScreenshotAdminDao? = null

    fun listThemeScreenshotAdminService(tid: String): List<ThemeScreenshot>? {
        return if (CheckDataUtils.stringUtilsID(tid)) {
            themeScreenshotAdminDao!!.listThemeScreenshotAdminDao(tid)
        } else null
    }

    /**
     * 删除图片
     *
     * @param id 截图id
     * @param tid 主题Id
     * @return
     */
    fun deleteThemeScreenshotAdminService(id: String, tid: String): Boolean {
        if (CheckDataUtils.stringUtilsID(id, tid)) {
            var fileName = themeScreenshotAdminDao!!.themeScreenshotByIdDao(id)
            fileName = FinalStringUtils.THEME + tid + "/" + fileName
            val bool = themeScreenshotAdminDao!!.deleteThemeScreenshotAdminDao(tid, id) > 0
            ImageLookUtils.deleteImage(fileName)
            return bool
        }
        return false

    }

    fun showImageService(imageName: String, tid: String): ByteArray? {
        return ImageLookUtils.readImage(FinalStringUtils.THEME + tid + "/", imageName)
    }

    fun submitThemeScreenshotAdminService(themeScreenshot: ThemeScreenshot, image: CommonsMultipartFile): Boolean {
        val tid = themeScreenshot.tid ?: ""
        if (CheckDataUtils.stringUtilsID(tid)) {// 检测图片的主题的id是否有效
            val isuffix = ImageLookUtils.getFileSuffix(image)
            if (CheckDataUtils.iconUtils(isuffix!!)) {//检测截图是否是指定的图片后缀
                val imageName = DatabaseIdUtils.dataIdOne + isuffix
                if (ImageLookUtils.saveFile(FinalStringUtils.THEME + tid + "/" + imageName, image)) { //保存截图
                    themeScreenshot.id = DatabaseIdUtils.dataIdOne// 生成图片名称
                    themeScreenshot.date = Date()
                    themeScreenshot.image = imageName
                    //保存到数据库中
                    return this.themeScreenshotAdminDao!!.saveMassageDao(themeScreenshot) > 0
                }


            }


        }
        return false

    }

    /**
     * 修改图片
     */
    fun changeThemeScreenshotAdminService(file: CommonsMultipartFile, id: String, tid: String, fileName: String): Boolean {
        val isuffix = ImageLookUtils.getFileSuffix(file)
        if (CheckDataUtils.iconUtils(isuffix!!)) {
            val imageName = DatabaseIdUtils.dataIdOne + isuffix
            if (ImageLookUtils.saveFile(FinalStringUtils.THEME + tid + "/" + imageName, file)) { //保存截图
                //保存到数据库中
                val bool = this.themeScreenshotAdminDao!!.changeThemeScreenshotAdminDao(id, imageName) > 0
                if (bool)
                    ImageLookUtils.deleteImage(FinalStringUtils.THEME + tid + "/" + fileName)
                return bool
            }
        }

        return false
    }

    fun changeThemeScreenshotInfoAdminService(type: Int, id: String, value: String): Boolean {
        val themeScreenshotUtils = ThemeScreenshotUtils(id = id, value = value, type = type)
        return this.themeScreenshotAdminDao!!.changeThemeScreenshotInfoAdminDao(themeScreenshotUtils) > 0

    }

    /**
     * 修改截图顺序
     */
    fun changeThemeScreenshotSoftAdminService(id: String): Boolean =
            this.themeScreenshotAdminDao!!.changeThemeScreenshotSoftAdminDao(id) > 0
}
